package com.dog.basics.admin.security;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.basics.bean.model.sys.SysMenu;
import com.dog.basics.bean.model.sys.SysUser;
import com.dog.basics.constant.Constant;
import com.dog.basics.dao.sys.SysMenuMapper;
import com.dog.basics.dao.sys.SysUserMapper;
import com.dog.basics.security.enums.App;
import com.dog.basics.security.exception.UsernameNotFoundExceptionBase;
import com.dog.basics.security.model.AppConnect;
import com.dog.basics.security.service.DogSysUser;
import com.dog.basics.security.service.DogUser;
import com.dog.basics.security.service.DogUserDetailsService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户详细信息
 *
 * @author
 */
@Slf4j
@Service
@AllArgsConstructor
public class DogSysUserDetailsServiceImpl implements DogUserDetailsService {
    private final SysMenuMapper sysMenuMapper;
    private final SysUserMapper sysUserMapper;

    /**
     * 用户密码登录
     *
     * @param username 用户名
     * @return
     * @throws UsernameNotFoundExceptionBase
     */
    @Override
    @SneakyThrows
    public DogSysUser loadUserByUsername(String username) {
        return getUserDetails(username);
    }


    /**
     * 构建userdetails
     *
     * @param username 用户名称
     * @return
     */
    private DogSysUser getUserDetails(String username) {
        SysUser sysUser = sysUserMapper.selectByUsername(username);

        if (sysUser == null) {
            throw new UsernameNotFoundExceptionBase("用户不存在");
        }
        Collection<? extends GrantedAuthority> authorities
                = AuthorityUtils.createAuthorityList(getUserPermissions(sysUser.getUserId()).toArray(new String[0]));
        // 构造security用户
        return new DogSysUser(sysUser.getUserId(), sysUser.getAccountType(), sysUser.getUsername(), sysUser.getPassword(), sysUser.getStatus() == 1,
                true, true, true, authorities);
    }

    private Set<String> getUserPermissions(Long userId) {
        List<String> permsList;

        //系统管理员，拥有最高权限
        if (userId == Constant.SUPER_ADMIN_ID) {
            List<SysMenu> menuList = sysMenuMapper.selectList(Wrappers.emptyWrapper());


            permsList = menuList.stream().map(SysMenu::getPerms).collect(Collectors.toList());
        } else {
            permsList = sysUserMapper.queryAllPerms(userId);
        }


        Set<String> permsSet = permsList.stream().flatMap((perms) -> {
                    if (StrUtil.isBlank(perms)) {
                        return null;
                    }
                    return Arrays.stream(perms.trim().split(","));
                }
        ).collect(Collectors.toSet());
        return permsSet;
    }

    @Override
    public DogUser loadUserByAppIdAndBizUserId(App app, String bizUserId) {
        return null;
    }

    @Override
    public DogUser loadUserBypasswordOrEmail(String code) {
        return null;
    }

    @Override
    public DogUser loadUserByWxPhone(String mobile, String openId, String uuind, Integer from) {
        return null;
    }

    @Override
    public DogUser loadUserByWxOpenId(String openId, Integer type, String uuind, Integer from) {
        return null;
    }



    @Override
    public DogUser loadUserByAppOpenId(String code) {
        return null;
    }


    @Override
    public DogUser loadUserByminiOpenId(String code) {
        return null;
    }

    @Override
    public void insertUserIfNecessary(String phone, String openId, Integer type) {

    }

    @Override
    public void insertUserConnect(AppConnect appConnect) {

    }

}
